<?php

namespace app\common\service;

class HostModuleRelation extends Base 

{

   /**
    * @name: 添加主机初始化
    * @Param: 
    * @Description: 
    * @Author: foo
    * @Date: 2019-12-10 17:11:39
    */
   function init ($host_id)
   {
      db('HostModuleRelation')->insert(['host_id' => $host_id]);
   }

   /**
    * @name: 卸载客户端
    * @Param: 
    * @Description: 
    * @Author: foo
    * @Date: 2019-12-10 17:57:56
    */
   function uninstall ($host_id_a)
   {
      db('HostModuleRelation')->whereIn('host_id', $host_id_a)->delete();
   }
   /**
    * @name: 添加主机到模块
   * @Param: 
   * @Description: 
   * @Author: foo
   * @Date: 2019-12-02 15:15:28
   */
   function add ($host_id_a, $module)
   {
      db('HostModuleRelation')->whereIn('host_id', $host_id_a)->update([$module => 1]);
   }

   /**
    * @name: 删除模块中的 主机
    * @Param: 
    * @Description: 
    * @Author: foo
    * @Date: 2019-12-02 15:15:59
    */
   function del ($host_id_a, $module)
   {
      db('HostModuleRelation')->whereIn('host_id', $host_id_a)->update([$module => 0]);
   }

   /**
    * @name: 获取表字段
    * @Param: 
    * @Description: 
    * @Author: foo
    * @Date: 2019-12-02 15:18:39
    */
   function getFields()
   {
      $field = db()->query('SHOW COLUMNS FROM ' . config('database.prefix') . 'host_module_relation' );
      if (empty($field))
      {
            $this->setError( $this->CODE_FAIL, '系统错误!!请联系技术员');
            return [];
      }

      $return = array_column($field, 'Field');

      if(count($return) <= 2)
      {
         $this->setError( $this->CODE_FAIL, '该系统未添加任意模块!!请联系管理员');
         return [];
      }
      
      return $return;
   }

   /**
    * @name: 模块检测
    * @Param: 
    * @Description: 
    * @Author: foo
    * @Date: 2019-12-02 15:25:51
    */
   function fieldCheck($module)
   {
      $return = $this->getFields();
      if(!array_search($module, $return))
      {
         $this->setError($this->CODE_FAIL, '未找到该模块信息');
         return [];
      }

      return $module;
   }

   /**
    * @name: 剩余添加数量
    * @Param: 
    * @Description: 
    * @Author: foo
    * @Date: 2019-12-03 15:44:19
    */
   function existNum ($module)
   {
      $return = db('HostModuleRelation')->where($module, 1)->count();

      return $return;
   }

   /**
    * @name: 检测模块冲突
    * @Param: 
    * @Description: 
    * @Author: foo
    * @Date: 2019-12-04 14:31:05
    */
   function checkConflict ($module)
   {
      $host_a = $this->getHost();
      $conflict_a = [
         'netfilter' => [
            'hgshield',
         ],
         'hgshield' => [
            'netfilter',
         ]
      ];
      $return = 0;
      if(isset($conflict_a[$module]))
      {
         foreach ($host_a as $v)
         {
            $server = db('HostModuleRelation');
            foreach ($conflict_a[$module] as $cv)
            {
               $server->whereOrRaw('host_id='. $v['id'] . ' AND ' . $cv . '=1');
            }

            if ($return = $server->count())
            {
                $hostStr_a[$v['id']] = $v['id'];
            }
         }
         
      }

      if($module == 'hgshield'){
           $conflictModule = 'Netfilter';
      }else{
           $conflictModule = 'hgshild';
      }


      if($return > 0)
      {
          $msg = '主机已存在于'.$conflictModule.' 模块中，请先在'.$conflictModule.' 模块中移除后再添加至本模块！';
          $message = $this->hostErrorMessage($hostStr_a, $msg);
          $this->setError($this->CODE_FAIL, $message);
         return false;
      }

      return $return == 0;
   }

    /**
     * @name 系统伪装依赖关系
     * @author wx
     * @date 2020/1/7 18:39
     */
   function checkRelyOn($module, $host_id_os)
   {
       if ($module != 'disguise') {
           return true;
       }

       $host_id_arr = array();
       foreach ( $host_id_os as $host_id => $os){
            if($os == 2){
                $host_id_arr[] = $host_id;
            }
       }
       if(empty($host_id_arr)){
           return true;
       }
       $list = db('HostModuleRelation')->where('host_id','in',$host_id_arr)->select();

       $noInit = array();
       foreach ($list as $k => $v){
           if($v['hgshield'] == 0 && $v['netfilter'] == 0){
              $noInit[] = $v['host_id'];
           }
       }

       if(!empty($noInit)){
           $message = $this->hostErrorMessage($noInit,'开启系统伪装功能需依赖Netfilter模块或函谷盾模块，请先添加对应模块！');
           $this->setError($this->CODE_FAIL, $message);
           return false;
       }

   }

   /**
    * @name: 检测开启
    * @Param: 
    * @Description: 
    * @Author: foo
    * @Date: 2019-12-04 16:09:09
    */
   function checkStatus ($column, $status)
   {
      $host_a = $this->getHost('id');
      $host_id_a = db('HostModuleRelation')->where($column, $status)->whereIn('host_id', $host_a)->column('host_id');
      if(!empty($host_id_a))
      {
         $host_a = $this->getHost();
         foreach ($host_id_a as $v)
         {
            $hostStr_a[] = $host_a[$v]['name'] ;
         }
         $statusStr = $status ? '开启' : '关闭';
         $this->setError($this->CODE_FAIL, '主机 [' . implode(']，[', $hostStr_a) . '] 已' . $statusStr . '该模块');
      }
   }

   /**
    * @name:查找单个 依据主机id
    * @Param: 
    * @Description: 
    * @Author: foo
    * @Date: 2019-12-07 14:20:44
    */
   function findByHostId ($host_id)
   {
      $return = db('HostModuleRelation')->where('host_id', $host_id)->find();

      return $return;
   }

    /**
     * @name 检测netfilter或者hgshield的状态
     * @author wx
     * @date 2020/1/7 18:25
     */
   function findModuleStatus($host_id)
   {
        $info = db('HostModuleRelation')->where('host_id',$host_id)->find();
        if($info['netfilter'] == 0 &&  $info['hgshield'] == 0){
            return false;
        }else{
            return true;
        }
   }
}